// Figures out the event we are using with the bound element
// https://github.com/dhershman1/vue-debounce
// https://blog.csdn.net/sinat_36146776/article/details/84953131

import {debounce} from 'throttle-debounce'
import {on, off} from '/src/utils/dom.js'

let fn = null
export default {
  name: 'debounce',
  install(app, { defaultTime = 300 } = {}) {
    app.directive('debounce', {
      created(el, {value, arg, modifiers}) {
      },
      mounted(el, {value, arg, modifiers}) {
        fn = debounce(arg || defaultTime, value)
        on(el, 'click', fn)
      },
      unmounted: function (el) {
        if (fn) {
          off(el, 'click', fn)
        }
      }
    })
  }

}
